var sortationBar = null;
var identityBar = null;
var areaBar = null;
var statusBar = null;
var timeBar = null;

document.ready(function() {
	webSpinner.spin(getItem('loadSpin'));

    setValue('menuid', getUrlParam('menuid'));
    loadData();

    setDisplay('loadSpin', 'none');
    setDisplay('mainBg', '');
    webSpinner.spin();
});

function loadData() {
    var list = '';

    list = [
        {value:'0', text:'月嫂保姆'}, 
        {value:'1', text:'清洗保洁'}, 
        {value:'2', text:'家居清洗'}];
    sortationBar = getSelectItemList('sortationText', 'sortationValue', list);

    list = [
        {value:'0', text:'个人'}, 
        {value:'1', text:'家政公司'}];
    identityBar = getSelectItemList('identityText', 'identityValue', list);

    list = [
        {value:'', text:'海城区'}, 
        {value:'', text:'银海区'}, 
        {value:'', text:'铁山港区'}, 
        {value:'', text:'合浦县'}];
    areaBar = getCheckItemBar('area', '', list);

    list = [
        {value:'1', text:'展示中'}, 
        {value:'0', text:'待审核'}];
    statusBar = getSelectItemList('statusText', 'statusValue', list);

    list = [
        {value:'30', text:'1个月'}, 
        {value:'92', text:'3个月'}, 
        {value:'183', text:'半年'}, 
        {value:'365', text:'1年'}, 
        {value:'3650', text:'不限制'}];
    timeBar = getSelectItemList('showtime', 'expiretime', list);
}

function back() {
    toUrl('console.php?m=housecraft&c=list&menuid=' + getValue('menuid'));
}

function selectSortation() {
    openSlipSelectDiv(getValue('sortationText'), sortationBar);
}

function showIdentity() {
    if (getValue('sortationValue') == '0') {
        setDisplay('identityDiv', '');
    }
    else {
        setDisplay('identityDiv', 'none');
        setValue('identityText', '');
        setValue('identityValue', '');
    }
}

function selectIdentity() {
    openSlipSelectDiv(getValue('identityText'), identityBar);
}

function checkArea() {
    openSlipCheckDiv(getValue('area'), areaBar);
}

function selectTime() {
    openSlipSelectDiv(getValue('showtime'), timeBar);
}

function getExpiretime() {
    var days = getValue('expiretime');
    setValue('expiretime', get_add_day(days));
}

function selectStatus() {
    openSlipSelectDiv(getValue('statusText'), statusBar);
}

function saveHousecraft() {
    var list = [
        {id:'title', name:'标题', rule:'wick', empty:false}, 
        {id:'sortationValue', name:'分类', rule:'', empty:false}, 
        {id:'address', name:'商家地址', rule:'wick', empty:true}, 
        {id:'person', name:'联系人', rule:'wick', empty:false}, 
        {id:'phone', name:'联系电话', rule:'contact', empty:false}, 
        {id:'remark', name:'备注', rule:'wick', empty:true}, 
        {id:'feature', name:'服务特征', rule:'wick', empty:true}, 
        {id:'area', name:'服务范围', rule:'wick', empty:true}, 
        {id:'introduce', name:'服务内容', rule:'', empty:false}, 
        {id:'showtime', name:'展示时长', rule:'', empty:false}];
    if (checkParam(list)) {
        return;
    }
    if (getValue('sortationValue') == '0' && isEmpty('identityValue')) {
        showTip('月嫂保姆类必须选择服务者身份');
        return;
    }

    var json = [];
    json.push(fileList);

    var param = {};
    param.team = 'console';
    param.ctrl = 'housecraft';
    param.func = 'saveHousecraft';
    param.flag = 'add';
    param.title = getAjaxValue('title');
    param.sortation = getAjaxValue('sortationValue');
    param.address = getAjaxValue('address');
    param.person = getAjaxValue('person');
    param.phone = getAjaxValue('phone');
    param.identity = getAjaxValue('identityValue');
    param.feature = getAjaxValue('feature');
    param.area = getAjaxValue('area');
    param.introduce = getAjaxValue('introduce');
    param.expiretime = getAjaxValue('expiretime');
    param.status = getAjaxValue('statusValue');
    param.remark = getAjaxValue('remark');
    param.bigpic = JSON.stringify(json);
    param.coverid = getAjaxValue('coverPicId');
    
    sendAjax(param, function(result) {
        submitRecord('add', '家政服务添加页', '创建', result.housecraftid);
        back();
    });
}